package com.mediator.smartHouse;

import java.util.HashMap;

public class ConcreteMediator extends Mediator {

    //用来存放所有的同事
    private HashMap<String, Colleague> colleagues;
    //这个集合是用来干嘛的？？ 也是用来存放具体的咖啡机的
    //键存的事同事的类型，值存的是同事在 colleagues中的键值
    private HashMap<String, String> interMap;

    //构造
    public ConcreteMediator() {
        this.colleagues = new HashMap<>();
        this.interMap = new HashMap<>();
    }

    @Override
    public void registerColleague(Colleague colleague) {
        this.colleagues.put(colleague.getName(), colleague);

        if (colleague instanceof AlarmColleague) {
            interMap.put("AlarmColleague", colleague.getName());
        } else if (colleague instanceof CoffeeMachineColleague) {
            interMap.put("CoffeeMachineColleague", colleague.getName());
        } else if (colleague instanceof TVColleague) {
            interMap.put("TVColleague", colleague.getName());
        } else if (colleague instanceof CurtainsColleague) {
            interMap.put("CurtainsColleague", colleague.getName());
        } else {
            throw new RuntimeException("未知同事！！！");
        }
    }

    @Override
    public void getMessage(int stateChange, String name) {
        //检查是那个同事发送的消息，不同的同事执行不同的逻辑
        //通过设备名拿到设备对象
        Colleague colleague = colleagues.get(name);
        //判断这个和对象是具体是那个同事
        if (colleague instanceof AlarmColleague) {
            //如果是闹钟 检查闹钟当前的状态
            if (stateChange == 0) {
                //让闹钟调用咖啡机对象 要通过innerMap得到咖啡机对象在 colleagus中的键值
                String coffeMachineName = interMap.get("CoffeeMachineColleague");
                CoffeeMachineColleague coffeeColleague = (CoffeeMachineColleague) colleagues.get(coffeMachineName);
                coffeeColleague.startCoffee();

                //让电视机开始工作
                TVColleague tvColleague = (TVColleague) colleagues.get(interMap.get("TVColleague"));
                tvColleague.openTv();

                //让窗帘降下
                CurtainsColleague curtainsColleague = (CurtainsColleague) colleagues.get(interMap.get("CurtainsColleague"));
                curtainsColleague.down();
            }
        } else if (colleague instanceof CoffeeMachineColleague) {
            //如果是咖啡机检测咖啡机的状态变为开还是关 不管其他的设备了
            CoffeeMachineColleague coffee = (CoffeeMachineColleague) colleague;
            if (stateChange == 0) {//开了
                coffee.startCoffee();
            } else {
                coffee.closeCoffee();
            }
        } else if (colleague instanceof TVColleague) {
            //如果是电视机，检测电视的状态变为开还是关 不管其他的设备了
            TVColleague tv = (TVColleague) colleague;
            if (stateChange == 0) {//开了
                tv.openTv();
            } else {
                tv.closeTv();
            }
        } else if (colleague instanceof CurtainsColleague) {
            //窗帘打开的时候不在管其他的设备了
            CurtainsColleague curtains = (CurtainsColleague) colleague;
            if (stateChange == 0) {//开了
                curtains.down();
            } else {
                curtains.up();
            }
        }
    }

    @Override
    public void sendMessage() {

    }
}
